package dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.activity_tally;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.R;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.fragment_bill.ClassifyFragment;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.fragment_bill.DetailFragment;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.fragment_bill.TrendFragment;

public class BillActivity extends AppCompatActivity implements View.OnClickListener{

    private DetailFragment detailFragment;
    private TrendFragment trendFragment;
    private ClassifyFragment classifyFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);

        LinearLayout ll_detail = findViewById(R.id.ll_detail);
        LinearLayout ll_statistics = findViewById(R.id.ll_statistics);
        LinearLayout ll_asset = findViewById(R.id.ll_asset);

        ll_detail.setOnClickListener(this);
        ll_statistics.setOnClickListener(this);
        ll_asset.setOnClickListener(this);

        initFragment(0);
        Log.d("tag","BillActivity create");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_detail:
                initFragment(0);
                break;
            case R.id.ll_statistics:
                initFragment(1);
                break;
            case R.id.ll_asset:
                initFragment(2);
                break;
            default:
                break;
        }
    }

    public void initFragment(int index){
        String phoneNumber = getIntent().getStringExtra("phone_toBA");
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (index){
            case 0:
                if(detailFragment == null){
                    detailFragment = new DetailFragment(phoneNumber);
                    transaction.add(R.id.fl_bill, detailFragment,null);
                }
                else{
                    transaction.show(detailFragment);
                }
                break;
            case 1:
                if(classifyFragment == null){
                    classifyFragment = new ClassifyFragment(phoneNumber);
                    transaction.add(R.id.fl_bill, classifyFragment,null);
                }
                else{
                    transaction.show(classifyFragment);
                }
                break;
            case 2:
                if(trendFragment == null){
                    trendFragment = new TrendFragment(phoneNumber);
                    transaction.add(R.id.fl_bill,trendFragment,null);
                }
                else{
                    transaction.show(trendFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        if (detailFragment != null) {
            transaction.hide(detailFragment);
        }
        if (classifyFragment != null) {
            transaction.hide(classifyFragment);
        }
        if (trendFragment != null) {
            transaction.hide(trendFragment);
        }
    }

    @Override
    public void finish() {
        super.finish();
        Log.d("tag","BillActivity finish");
    }
}